package com.lc.command;

import com.lc.protocol.DeviceError;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zhyioo on 2017/4/29.
 */

public class CommandActuator {

    public static int LogErr(JSONObject rlt, int code, String msg) {
        try {
            rlt.put("CODE", code);

            if (msg.length() > 0 && code != DeviceError.Success.ordinal()) {
                String oldMsg = rlt.optString("MSG", "");
                if(oldMsg.length() > 0) {
                    oldMsg += " <- ";
                    oldMsg += msg;
                    rlt.put("MSG", oldMsg);
                } else {
                    rlt.put("MSG", msg);
                }
            }
        } catch (JSONException e) {

        }
        return code;
    }

    public static int LogErr(JSONObject rlt, DeviceError err, String msg) {
        return LogErr(rlt, err.ordinal(), msg);
    }

    protected ICommandDriver _handler = null;

    public CommandActuator() {}

    public CommandActuator(ICommandDriver handler) {
        _handler = handler;
    }

    public void SetHandler(ICommandDriver handler) {
        _handler = handler;
    }

    protected JSONObject _arg = new JSONObject();

    protected JSONObject _rlt = new JSONObject();

    public JSONObject Argument() {
        return _arg;
    }

    public JSONObject Result() {
        return _rlt;
    }

    public CommandActuator Arg(String key, int val) {
        try {
            _arg.put(key, val);
        } catch (JSONException e) {

        }
        return this;
    }

    public CommandActuator Arg(String key, String val) {
        try {
            _arg.put(key, val);
        } catch (JSONException e) {

        }
        return this;
    }

    public CommandActuator Arg(String key, boolean val) {
        try {
            _arg.put(key, val);
        } catch (JSONException e) {

        }
        return this;
    }

    public CommandActuator Arg(String key, byte val) {
        try {
            _arg.put(key, (int)val);
        } catch (JSONException e) {

        }
        return this;
    }

    public CommandActuator Arg(String sJson) {
        return this;
    }

    public CommandActuator Reset() {
        _arg = new JSONObject();
        _rlt = new JSONObject();

        return this;
    }

    public CommandActuator Call(String cmd) {
        OnCall(cmd);
        return this;
    }

    public boolean OnCall(String cmd) {
        return OnCall(cmd, _arg, _rlt);
    }

    public boolean OnCall(String cmd, JSONObject arg, JSONObject rlt) {
        boolean bOK = _handler.OnCommand(cmd, arg, rlt);
        if(bOK) {
            try {
                rlt.put("CODE", 0);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            LogErr(rlt, DeviceError.OperatorErr, "OnCall:" + cmd);
        }
        return bOK;
    }

    public boolean IsOK() {
        if(_rlt == null) {
            return false;
        }
        int unknownErr = DeviceError.UnKnownErr.ordinal();
        int code = _rlt.optInt("CODE", unknownErr);
        return code == DeviceError.Success.ordinal();
    }
}
